// Copyright (c) 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

/**
*@describe:
*@author wfl19/Kristas
*@date 2021/08/27
 */

package api_test

import (
	"errors"
	"gitee.com/kristas/booting-go/framework"
	"gitee.com/kristas/booting-go/framework/core/statement/types"
	"gitee.com/kristas/booting-go/framework/web/resp"
	. "gitee.com/kristas/booting-go/framework/web/rest"
	"net/http"
)

func init() {
	framework.Component(new(TestErrorController))
}

type TestErrorController struct {
	types.Controller    `group:"/test/error"`
	errorWriter         GET `url:"/writer/error" param:"[w response]"`
	internalErrorWriter GET `url:"/writer/internal_error" param:"[w response]"`
	badRequestWriter    GET `url:"/writer/bad_request" param:"w response"`
	error               GET `url:"/error"`
	internalError       GET `url:"/internal_error"`
	badRequest          GET `url:"/bad_request"`
}

func (r *TestErrorController) ErrorWriter(w http.ResponseWriter) {
	resp.Error(http.StatusNotFound, errors.New("~~404 not found~~")).Write(w)
}

func (r *TestErrorController) InternalErrorWriter(w http.ResponseWriter) {
	resp.ErrInternalError(errors.New("~~internal server error~~")).Write(w)
}

func (r *TestErrorController) BadRequestWriter(w http.ResponseWriter) {
	resp.ErrBadRequest("~~bad request~~").Write(w)
}

func (r *TestErrorController) Error() (interface{}, error) {
	return nil, resp.Error(http.StatusNotFound, errors.New("~~404 not found~~"))
}

func (r *TestErrorController) InternalError() (interface{}, error) {
	return nil, resp.ErrInternalError(errors.New("~~internal server error~~"))
}

func (r *TestErrorController) BadRequest() (interface{}, error) {
	return nil, resp.ErrBadRequest("~~bad request~~")
}
